﻿using System;
using System.Collections.Generic;
using System.Text;

using log4net;
using log4net.Config;
using log4net.Repository.Hierarchy;
using log4net.Appender;
using System.Web.Configuration;

namespace QuickDev.Common.Audit
{
    public class Logger
    {
        #region Constants

        private const string APPENDER_NAME = "AdoNetAppender";
        private const string LOGGER_NAME = "LOGGER_NAME";

        #endregion

        #region Member Variables

        private static readonly ILog _log = LogManager.GetLogger( WebConfigurationManager.AppSettings[LOGGER_NAME] );

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="T:Logger"/> class.
        /// </summary>
        private Logger()
        {
        }

        /// <summary>
        /// Initializes the <see cref="T:Logger"/> class.
        /// </summary>
        static Logger()
        {
            XmlConfigurator.Configure();
            Hierarchy hierarchy = LogManager.GetRepository() as Hierarchy;
            if ( hierarchy != null )
            {
                AdoNetAppender adoNetAppender = ( AdoNetAppender )hierarchy.GetLogger( WebConfigurationManager.AppSettings[LOGGER_NAME] , hierarchy.LoggerFactory ).GetAppender( APPENDER_NAME );
                if ( adoNetAppender != null )
                {
                    adoNetAppender.ConnectionString = WebConfigurationManager.ConnectionStrings[WebConfigurationManager.AppSettings[LOGGER_NAME]].ConnectionString;
                    adoNetAppender.ActivateOptions();
                }
            }
        }

        #endregion

        #region Methods

        #region Public

        public static void Debug( object message )
        {
            _log.Debug( message );
        }

        public static void Debug( object message , Exception ex )
        {
            _log.Debug( message , ex );
        }

        public static void Error( string message )
        {
            _log.Error( message );
        }

        public static void Error( object message , Exception ex )
        {
            _log.Error( message , ex );
        }

        public static void Information( string message )
        {
            _log.Info( message );
        }

        public static void Information( string message , Exception ex )
        {
            _log.Info( message , ex );
        }

        #endregion

        #endregion
    }
}
